<?php
/**
 * created by zzy
 * date: 2017/11/21 10:39
 */

namespace App\Http\Admin\System\Controllers;

use Framework\BaseClass\Http\Admin\Controller;
use App\Eloquent\Ygt\AdminRole;
use App\Eloquent\Ygt\AdminRoleMenu;
use App\Eloquent\Ygt\AdminMenu;

class RoleController extends Controller
{
    public function index(){}
    public function lists(){

        if (request()->isMethod('post')) {

            $pageSize       = request('pageSize', 30);
            $pageCurrent    = request('pageCurrent', 1);
            $offset         = ($pageCurrent - 1) * $pageSize;

            $where          = [];
            $lists          = AdminRole::getList( $where,'',$pageSize,$offset )->toArray();
            $count          = AdminRole::getCount($where);
            $data           = ['totalRow'=>$count,'pageCurrent'=>$pageCurrent,'list'=>$lists];
            $data_json      = json_encode($data);
            return $data_json;
        }
        return $this->view('lists' );
    }
    public function edit(){

        $params             = $this->requestJson();
        $id                 = isset($params['id']) ? $params['id'] : 0;
        $name               = $params['name'];
        $display_name       = $params['display_name'];
        $description        = $params['description'];

        $data               = ['name'=>$name,'display_name'=>$display_name,'description'=>$description];
        if( $id ){
            $where          = ['id'=>$id];
            $result         = AdminRole::updateOneData( $where, $data );
        } else {
            $result         = AdminRole::insertOneData( $data );
        }

        if( $result ){
            return $this->ajaxSuccess( '成功' );
        } else {
            return $this->ajaxFail( '失败' );
        }
    }
    public function delete(){
        $id                 = request('id',0);
        if( $id ){
            $id_arr         = explode(',',$id);
            $where          = ['id'=>['in',$id_arr]];
            $del_result     = AdminRole::del($where);
            if( $del_result !== false ){
                return $this->ajaxSuccess('成功');
            } else {
                return $this->ajaxFail('失败2');
            }
        } else {
            return $this->ajaxFail('失败');
        }
    }
    public function menu(){

        $role_id                = request( 'id' );
        if (request()->isMethod('post')) {
            $id_arr             = request( 'id_arr' );
            if( $id_arr ){
                $where          = ['admin_role_id'=>$role_id];
                $del_result     = AdminRoleMenu::del($where);
                if( $del_result !== false ){
                    foreach( $id_arr as $key=>$val ){
                        $data       = ['admin_role_id'=>$role_id,'admin_menu_id'=>$val];
                        AdminRoleMenu::insertOneData($data);
                    }
                    return $this->ajaxSuccess('成功');
                } else {
                    return $this->ajaxFail('失败删');
                }
            } else {
                return $this->ajaxFail('失败空');
            }
        } else {
            $info               = AdminRole::getInfo(['id'=>$role_id]);
            $menu_list          = AdminMenu::getList('','id,pid,name');
            $menu_list_arr      = $menu_list->toArray();
            $menu_top           = $top_id_arr = $menu_parent = $menu_son = $menu_arr = [];
            foreach( $menu_list_arr as $key=>$val){
                if( $val['pid'] == 0 ){
                    $menu_top[]                  = $val;
                    $top_id_arr[]               = $val['id'];
                } else {
                    $menu_arr[$val['id']]       = $val;
                }
            }
            foreach( $menu_arr as $key=>$val ) {
                if ( in_array( $val['pid'], $top_id_arr ) ) {
                    $menu_parent[]              = $val;
                    unset($menu_arr[$key]);
                } else {
                    $menu_son[$val['pid']][]        = $val;
                }
            }
//            foreach( $menu_son as $key=>$val ){
//                echo $key.'<br/>';
//                print_r($val);
//                echo '<hr/>';
//            }exit;
            $role_menu_list     = AdminRoleMenu::getList(['admin_role_id'=>$role_id],'admin_role_id,admin_menu_id');
            $role_menu_arr      = $role_menu_list->groupBy('admin_menu_id')->keys()->toArray();
            return $this->view('menu', compact('info','role_menu_arr','menu_top','menu_parent','menu_son') );
        }
    }
}
